<template>
  <div class="app-container">
    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="default"
          @click="handleAdd"
        >
          {{ $t("form.evaluation.addRole") }}
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="evalRoleList"
    >
      <el-table-column
        :label="$t('form.evaluation.name')"
        align="center"
        prop="name"
      />
      <!--      <el-table-column-->
      <!--        label="关联题目id"-->
      <!--        align="center"-->
      <!--        prop="linkItemList"-->
      <!--      />-->
      <el-table-column
        :label="$t('form.evaluation.operation')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="ele-Edit"
            @click="handleUpdate(scope.row)"
          >
            {{ $t("form.evaluation.modify") }}
          </el-button>
          <el-button
            link
            type="primary"
            icon="ele-Delete"
            @click="handleDelete(scope.row)"
          >
            {{ $t("form.evaluation.delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改测评角色对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="evalRoleRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          :label="$t('form.evaluation.name')"
          prop="name"
        >
          <el-input
            v-model="form.name"
            :placeholder="$t('form.evaluation.enterName')"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            :loading="buttonLoading"
            type="primary"
            @click="submitForm"
          >
            {{ $t("form.evaluation.confirm") }}
          </el-button>
          <el-button @click="cancel">{{ $t("form.evaluation.cancel") }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="EvalRole" lang="ts">
import { addEvalRole, delEvalRole, EvalRole, getEvalRole, listEvalRole, updateEvalRole } from "@/api/evaluation/role";
import { nextTick, ref } from "vue";
import { MessageUtil } from "@/utils/messageUtil";
import { ElMessageBox, type FormInstance } from "element-plus";
import { useRoute } from "vue-router";
import { evaluationHook } from "@/views/form/evaluation/useEvaluation";
import { formatString, resetFormRef } from "@/utils/tduck";
import { ResultData } from "@/api/types";
import { i18n } from "@/i18n";

const { evalRoleList } = evaluationHook;

const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const title = ref("");

const form = ref<EvalRole>({ name: "" });
const queryParams = ref({
  formKey: ""
});
const rules = ref({
  formKey: [
    {
      required: true,
      message: i18n.global.t("form.evaluation.formKeyCannotBeEmpty"),
      trigger: "blur"
    }
  ],
  name: [
    {
      required: true,
      message: i18n.global.t("form.evaluation.enterName"),
      trigger: "blur"
    }
  ]
});

const route = useRoute();

const formKey = ref(route.query.key);

/** 查询测评角色列表 */
const getList = () => {
  loading.value = true;
  queryParams.value.formKey = formKey.value as string;
  listEvalRole(queryParams.value).then(response => {
    evalRoleList.value = response.data;
    loading.value = false;
  });
};

// 取消按钮
const cancel = () => {
  open.value = false;
};

/** 新增按钮操作 */
const handleAdd = () => {
  nextTick(() => {
    resetFormRef(evalRoleRef);
  });
  form.value = { id: undefined, name: "", linkItemList: [] };
  open.value = true;
  title.value = i18n.global.t("form.evaluation.addRole");
};

/** 修改按钮操作 */
const handleUpdate = (row: EvalRole) => {
  loading.value = true;
  getEvalRole(row.id).then((response: ResultData) => {
    loading.value = false;
    resetFormRef(evalRoleRef);
    open.value = true;
    form.value = response.data;
    title.value = i18n.global.t("form.evaluation.modifyEvaluationRole");
  });
};

const evalRoleRef = ref<FormInstance | null>(null);

/** 提交按钮 */
const submitForm = () => {
  form.value.formKey = formKey.value as string;
  evalRoleRef.value!.validate((valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      form.value.linkItemList = [];
      if (form.value.id != null) {
        updateEvalRole(form.value)
          .then(() => {
            MessageUtil.success(i18n.global.t("common.updateSuccess"));
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        addEvalRole(form.value)
          .then(() => {
            MessageUtil.success(i18n.global.t("common.addSuccess"));
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
};

/** 删除按钮操作 */
const handleDelete = (row: EvalRole) => {
  ElMessageBox.confirm(i18n.global.t("form.evaluation.confirmDeleteEvaluationRoleNumber", { name: row.name }))
    .then(() => {
      loading.value = true;
      return delEvalRole(row.id);
    })
    .then(() => {
      loading.value = true;
      getList();
      MessageUtil.success(i18n.global.t("common.deleteSuccess"));
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
};

getList();
</script>
